﻿using UnityEngine;
using System.Collections;

public partial class PoliceFSM
{
    
    //-----------------------------------IdleState------------------------------------------
    IEnumerator IdleState()
    {
       

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.IDLE);
        gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();
        

        do
        {
            //구현

            yield return null;

            MoveOrder();
            CoverEnable();
            CloseAtkEnable();
            AimEnable();
            Chat(CHATBUBBLE_STATE.NORMAL);
            ReloadEnalbe();
            SupplyEnable();
            CommonState();

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.IDLE);


        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);

    }

    //-----------------------------------MoveState------------------------------------------
    IEnumerator MoveState()
    {
        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.MOVE);
        gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();

        do
        {
            yield return null;

            FootStep();
            MoveOrder();
            CommonState();

            //AStar

            BASEUNIT_STATE CurrentStat = AStarMove();
            if (CurrentStat != BASEUNIT_STATE.MOVE)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = CurrentStat;
                break;
            }

            //End

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.MOVE);


      SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);

    }

    //-----------------------------------CoverState------------------------------------------
    IEnumerator CoverState()
    {
        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.COVER);
        gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();

        gameObject.GetComponent<UnitStatus>().m_unitData.m_bCoverd = true;


        Calculate.RotToDest(gameObject, gameObject.GetComponent<UnitStatus>().m_unitData.m_Cover.gameObject.transform.position);

        do
        {
            yield return null;

            if (GetComponent<UnitStatus>().m_unitData.m_Cover == null)
            {
                gameObject.GetComponent<UnitStatus>().m_unitData.m_bCoverd = false;
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
                break;
            }
            MoveOrder();
            CloseAtkEnable();
            AimEnable();
            CommonState();


        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.COVER);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }


    IEnumerator DamageState()
    {
        float fCurrentTime = 0.0f;

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.DAMAGE);
        gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();

        gameObject.transform.GetChild(1).gameObject.active = false;

        do
        {
            yield return null;

            MoveOrder();
            CommonState();

            fCurrentTime += Time.deltaTime;

            if (fCurrentTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length)
            {
                fCurrentTime = 0.0f;
                gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.IDLE;
                break;
            }
            

        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DAMAGE);

        gameObject.transform.GetChild(1).gameObject.active = true;
        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);
    }

    IEnumerator DeadState()
    {
        float fCurrentTime = 0.0f;

        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.DEAD);
        gameObject.transform.GetChild(0).GetChild(0).GetComponent<UnitHeadState>().SetHeadAnim();

        do
        {
            yield return null;

            fCurrentTime += Time.deltaTime;

            if (fCurrentTime >= gameObject.GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).length)
            {
                fCurrentTime = 0.0f;
                //시체로 변함
                GameSystem.GetInstance().DeleteThisUnit(gameObject.GetComponent<UnitStatus>());
                break;
            }


        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.DEAD);

    }


    IEnumerator SupplyState()
    {
        gameObject.GetComponent<Animator>().SetInteger("iAnimID", (int)BASEUNIT_STATE.SUPPLY);

        {

            yield return null;

            gameObject.GetComponent<UnitStatus>().m_unitData.iHavMagazine += 3;
            m_bSupply = false;

            m_vecDest = m_vecPrevPos;
            Calculate.AStarStart(gameObject, m_vecDest, ref m_BestList, m_iPrevTIleIdx, m_iPrevTileOption);
            m_BestList = gameObject.GetComponent<AStar>().GetBestList();

            gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState = BASEUNIT_STATE.MOVE;
           


        } while (gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState == BASEUNIT_STATE.SUPPLY);

        SetState(gameObject.GetComponent<UnitStatus>().m_unitData.m_CurrentUnitState);

    }
}
